﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using LuxuryResort.WebSite.LRMPDataService;

namespace LuxuryResort.WebSite.NationalAdmin
{
    public partial class ReportOccupancy : Page
    {
        private readonly RoomServiceClient _svc = new RoomServiceClient();

        private Guid UserID
        {
            get
            {
                if (ViewState["UserID"] == null && HttpContext.Current.User != null)
                {
                    string userName = HttpContext.Current.User.Identity.Name;
                    MembershipUser membershipUser = Membership.GetUser(userName);
                    if (membershipUser != null)
                    {
                        object userId = membershipUser.ProviderUserKey;
                        ViewState["UserID"] = userId;
                    }
                }
                return (Guid) ViewState["UserID"];
            }
        }

        private Guid ResortId
        {
            get { return new Guid(ConfigurationManager.AppSettings["ResortId"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtFromDate.Text =
                    Convert.ToDateTime(string.Format("{0}/1/{1}", DateTime.Now.Month, DateTime.Now.Year))
                        .ToString("MM/dd/yyyy");
                txtToDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
            }
        }

        protected void btnViewReport_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                DateTime fromDate = Convert.ToDateTime(txtFromDate.Text);
                DateTime toDate = Convert.ToDateTime(txtToDate.Text);

                IEnumerable<LRMPOccupancyReport> occupancies = GetOccupancyReport(fromDate, toDate);

                int i = occupancies.Sum(occupancy => occupancy.TotalDays);

                int roomCount = GetAllRoom().Count;
                int rate = i/(toDate.Subtract(fromDate).Days + 1*roomCount);

                lblOccupancyRate.Text = string.Format("{0}%", rate*100);
                lvOccupancies.DataSource = occupancies;
                lvOccupancies.DataBind();
            }
        }

        //protected void DataPagerOccupancies_PreRender(object sender, EventArgs e)
        //{
        //    var firstMonthDate = Convert.ToDateTime(string.Format("{0}/1/{1}", DateTime.Now.Month, DateTime.Now.Year));
        //    lvOccupancies.DataSource = GetOccupancyReport(firstMonthDate, DateTime.Now);
        //    lvOccupancies.DataBind();
        //}

        private IEnumerable<LRMPOccupancyReport> GetOccupancyReport(DateTime fromDateParam, DateTime toDateParam)
        {
            return _svc.GetOccupancyReport(UserID, ResortId, fromDateParam, toDateParam).ToList();
        }

        private List<Room> GetAllRoom()
        {
            return _svc.GetRooms(UserID, ResortId).ToList();
        }
    }
}